package com.alfred.springboot.elasticjob.lite.core.autoconfigure;

import com.alfred.springboot.elasticjob.lite.common.constants.StringPool;
import com.alfred.springboot.elasticjob.lite.common.initail.SpringContextHoderInitalizer;
import com.alfred.springboot.elasticjob.lite.core.process.ElasticJobAnnotationProcessor;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({ZookeeperProperties.class})
public class AnnoOfSimpleJobAutoConfiguration {

    private ZookeeperProperties zookeeperProperties;

    public AnnoOfSimpleJobAutoConfiguration(final ZookeeperProperties zookeeperProperties) {
        this.zookeeperProperties = zookeeperProperties;
    }

    /**
     * <pre>
     *     注入{@link ZookeeperConfiguration}
     *
     * @return zookeeperConfiguration
     * </pre>
     */
    @Bean
    @ConditionalOnMissingBean
    public ZookeeperConfiguration zookeeperConfiguration() {

        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(this.zookeeperProperties.getServers(), this.zookeeperProperties.getNamespace());
        zkConfig.setBaseSleepTimeMilliseconds(this.zookeeperProperties.getBaseSleepTimeMilliseconds());
        zkConfig.setMaxSleepTimeMilliseconds(this.zookeeperProperties.getMaxSleepTimeMilliseconds());
        zkConfig.setMaxRetries(this.zookeeperProperties.getMaxRetries());
        zkConfig.setSessionTimeoutMilliseconds(this.zookeeperProperties.getSessionTimeoutMilliseconds());
        zkConfig.setConnectionTimeoutMilliseconds(this.zookeeperProperties.getConnectionTimeoutMilliseconds());

        if (null != this.zookeeperProperties.getDigest()
                && this.zookeeperProperties.getDigest().trim().length() > 0) {
            zkConfig.setDigest(this.zookeeperProperties.getDigest());
        }

        return zkConfig;
    }

    /**
     * <pre>
     *     注入{@link ZookeeperConfiguration}
     *
     * @param zkConfiguration
     * @return zkRegistryCenter
     * </pre>
     */
    @Bean(initMethod = StringPool.METHOD_INIT)
    @ConditionalOnMissingBean
    @ConditionalOnBean(ZookeeperConfiguration.class)
    public ZookeeperRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration zkConfiguration) {
        return new ZookeeperRegistryCenter(zkConfiguration);
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringContextHoderInitalizer springContextHoderInitalizer() {
        return new SpringContextHoderInitalizer();
    }

    @Configuration
    protected static class AnnotationConfiguration {
        @Bean
        public ElasticJobAnnotationProcessor annotationProcessor(ApplicationContext applicationContext) {
            return new ElasticJobAnnotationProcessor(applicationContext);
        }
    }

}